package com.woniuxy.controller;

import com.woniuxy.service.RechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/recharge")
public class RechargeController {

    @Autowired
    private RechargeRecordService rechargeService;

    @PostMapping
    public ResponseEntity<?> recharge(@RequestBody Map<String, Object> payload) {
        try {
            Long customerId = Long.valueOf(payload.get("customerId").toString());
            BigDecimal rechargeAmount = new BigDecimal(payload.get("rechargeAmount").toString());
            Long paymentId = Long.valueOf(payload.get("paymentId").toString());

            rechargeService.recharge(customerId, rechargeAmount, paymentId);

            return ResponseEntity.ok("充值成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("充值失败: " + e.getMessage());
        }
    }
}